//
//  BIDPinViewController.m
//  theFifth
//
//  Created by 邵瑞 on 13-5-29.
//
//

#import "BIDPinViewController.h"
#import "XYPieChart.h"
#import "DatePicker.h"

#import "BIDBillsLookoverManager.h"
#import "BIDTypesManager.h"
#import "BIDATypeSumAmount.h"
#import "BIDBillsOfType.h"
#define kLeftButtton 100
#define kMiddleLabel 101
#define kRightButton 102
#define kYearLabel 103
#define kMonthLabel 104
#define kMinusYear 105
#define kMinusMonth 106
#define kMonthLast @"month"
#define kYearLast  @"year"
#define kTypeName  @"name"
@interface BIDPinViewController ()<XYPieChartDataSource,XYPieChartDelegate>
{
    DatePicker *datePicker;
    UIButton *detail;
    BOOL isPressDetail;
}
@property(strong,nonatomic) XYPieChart *pieChart;
@property(strong,nonatomic) NSMutableArray *slice;
@property(strong,nonatomic) NSMutableArray *color;
@property(strong,nonatomic) UILabel *typeName;
@end

@implementation BIDPinViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    //self.navigationController.navigationBarHidden=NO;
    [self initViewOfNav];
    [self initPieUI];
    if ([[NSUserDefaults standardUserDefaults] objectForKey:kTypeName]) {
        _typeName.hidden=NO;
        _typeName.text=[[NSUserDefaults standardUserDefaults] objectForKey:kTypeName];
        detail.hidden=NO;
    }
}
-(void)initViewOfNav
{
    UILabel *monthLabel=[[UILabel alloc] initWithFrame:CGRectMake(190, 10, 40, 30)];
    
    monthLabel.backgroundColor=[UIColor clearColor];
    monthLabel.tag=kMonthLabel;
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kMonthLast] integerValue]!=0) {
        monthLabel.text=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:kMonthLast]];
    }
    else{
        monthLabel.text=[NSString stringWithFormat:@"%d",[self getMonth]];}
    monthLabel.textAlignment=NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:monthLabel];
    [monthLabel release];
    
    UILabel *yearLabel=[[UILabel alloc]initWithFrame:CGRectMake(60, 10, 80, 30)];
    yearLabel.backgroundColor=[UIColor clearColor];
    yearLabel.tag=kYearLabel;
    yearLabel.shadowColor=[UIColor grayColor];
    yearLabel.shadowOffset=CGSizeMake(1, 1);
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kYearLast] integerValue]!=0) {
        yearLabel.text=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:kYearLast]];
    }

    
    else{
        yearLabel.text=[NSString stringWithFormat:@"%d",[self getYear]];}
    yearLabel.textAlignment=NSTextAlignmentCenter;
    [self.navigationController.navigationBar addSubview:yearLabel];
    [yearLabel release];
    
    UIButton *minusYear=[UIButton buttonWithType:UIButtonTypeCustom];
    minusYear.tag=kMinusMonth;

    minusYear.frame=CGRectMake(140, 10, 40, 30);
    [minusYear setTitle:@"－" forState:UIControlStateNormal];
    [minusYear addTarget:self action:@selector(minusAndAddMonth:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:minusYear];
    

    
    
    UIButton *monthUB=[UIButton buttonWithType:UIButtonTypeCustom];
    monthUB.backgroundColor=[UIColor clearColor];
    monthUB.tag=kRightButton;
    monthUB.frame=CGRectMake(250, 10, 40, 30);
    [monthUB addTarget:self action:@selector(minusAndAddMonth:) forControlEvents:UIControlEventTouchUpInside];
    [monthUB setTitle:@"+" forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:monthUB];
    
    

}
#pragma mark--get Month and Year
-(NSInteger)getYear
{
    NSDate* now = [NSDate date];
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    
    
    
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit ;
    
    NSDateComponents *dd = [cal components:unitFlags fromDate:now];
    
    NSInteger y = [dd year];
  //  NSInteger week=[dd week];
  //  int m = [dd month];
   // int week=[dd weekOfMonth];

//    int d = [dd day];
//
//    
//    int hour = [dd hour];
//    
//    int min = [dd minute];
//    
//    int sec = [dd second];
    return y;
}
-(NSInteger)getMonth
{
    NSInteger month=0;
    NSDate* now = [NSDate date];
    
    NSCalendar *cal = [NSCalendar currentCalendar];
    
    
    
    
    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;
    
    NSDateComponents *dd = [cal components:unitFlags fromDate:now];
    month=[dd month];
    return month;
}
#pragma mark --pie
-(void)initPieUI
{
    _pieChart=[[XYPieChart alloc] initWithFrame:CGRectMake((320 - 260) / 2, 11, 260, 260)];
    [_pieChart setDataSource:self];
    [_pieChart setDelegate:self];
    [_pieChart setPieCenter:CGPointMake(_pieChart.bounds.size.width / 2, _pieChart.bounds.size.height / 2)];
    
    [_pieChart setStartPieAngle:M_PI];
   // [_pieChart is];
  //  [_pieChart setShowPercentage:NO];
   // self.slice=[[NSMutableArray alloc] initWithObjects:@"2",@"2",@"3",@"5", nil];
    [self initPieData];
    self.color=[NSMutableArray arrayWithObjects:[UIColor grayColor],[UIColor greenColor],[UIColor redColor],[UIColor blackColor], [UIColor purpleColor],[UIColor orangeColor],nil];
    [self.view addSubview:_pieChart];
    [_pieChart reloadData];

}
-(void)initPieData
{
    UILabel *yearLabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kYearLabel];
    UILabel *monthLabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kMonthLabel];
    NSInteger month=monthLabel.text.integerValue;
    NSInteger year=yearLabel.text.integerValue;
    NSMutableArray *array=[[BIDBillsLookoverManager shareBillsLookoverManager] listOfAmountOfMainTypesInMonth:month andYear:year andInType:0];
    self.slice=array;
   }
#pragma mark---pie delegate
-(NSUInteger)numberOfSlicesInPieChart:(XYPieChart *)pieChart
{
    return [self.slice count];
}
-(CGFloat)pieChart:(XYPieChart *)pieChart valueForSliceAtIndex:(NSUInteger)index
{
    BIDATypeSumAmount *sum=[_slice objectAtIndex:index];
    return sum.aTypeAmounts;
}

-(UIColor *)pieChart:(XYPieChart *)pieChart colorForSliceAtIndex:(NSUInteger)index
{
	   NSInteger indexa = index%4;
	   //颜色的count
    return [self.color objectAtIndex:indexa];
    
}
-(void)pieChart:(XYPieChart *)pieChart didSelectSliceAtIndex:(NSUInteger)index
{
    if (_typeName.hidden) {
        _typeName.hidden=NO;
        detail.hidden=NO;
    }
    BIDATypeSumAmount *sum=[_slice objectAtIndex:index];
    NSInteger type_id=sum.type_id;
    _typeName.backgroundColor=[UIColor clearColor];
    _typeName.text=[[BIDTypesManager shareTypesManager] selectTypeName:type_id];

}
- (void)viewDidLoad
{
    [super viewDidLoad];

              [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"pin.png"]]];
   
// [self initPieUI];

   // [_pieChart reloadData];
    _typeName=[[UILabel alloc] initWithFrame:CGRectMake(100, 300, 120, 40)];
    _typeName.textAlignment=NSTextAlignmentCenter;
    _typeName.textColor=[UIColor blueColor];
    _typeName.hidden=YES;
     _typeName.backgroundColor=[UIColor clearColor];
    detail=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    detail.frame=CGRectMake(220, 300, 40, 40);
    [detail addTarget:self action:@selector(checkDetail) forControlEvents:UIControlEventTouchUpInside];
    detail.hidden=YES;
    [self.view addSubview:detail];
    [self.view addSubview:_typeName];
    // Do any additional setup after loading the view from its nib.
}
#pragma  mark----button 
-(void)minusAndAddMonth:(UIButton*)sender
{
    
     UILabel *yearlabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kYearLabel];
    UILabel *monthlabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kMonthLabel];

    if (sender.tag==kRightButton) {
 
    
        NSInteger month=monthlabel.text.integerValue;
        month+=1;
        if (month>12) {
            NSInteger year=yearlabel.text.integerValue;
            year+=1;
            yearlabel.text=[NSString stringWithFormat:@"%d",year];
            month-=12;
        }
        monthlabel.text=[NSString stringWithFormat:@"%d",month];
    }
    if (sender.tag==kMinusMonth) {
        NSInteger month=monthlabel.text.integerValue;
        month-=1;
        if (month==0) {
            NSInteger year=yearlabel.text.integerValue;
            year-=1;
            yearlabel.text=[NSString stringWithFormat:@"%d",year];
            month+=12;
        }
        monthlabel.text=[NSString stringWithFormat:@"%d",month];
    }
    _typeName.hidden=YES;
    detail.hidden=YES;
    [self initPieData];
    [_pieChart reloadData];
}
#pragma mark----remove navigation view
-(void)viewWillDisappear:(BOOL)animated
{
 //   UIButton *buttom=(UIButton*)[self.navigationController.navigationBar viewWithTag:kLeftButtton];
    UILabel *yearLabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kYearLabel];
     UILabel *monthLabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kMonthLabel];
 UIButton *month=(UIButton*)[self.navigationController.navigationBar viewWithTag:kRightButton];
    UIButton *minMonth=(UIButton*)[self.navigationController.navigationBar viewWithTag:kMinusMonth];
   // [buttom removeFromSuperview];
    [yearLabel removeFromSuperview];
    [monthLabel removeFromSuperview];
    [month removeFromSuperview];
    [minMonth removeFromSuperview];
    [_pieChart removeFromSuperview];
    _pieChart=nil;
    _typeName.hidden=YES;
        detail.hidden=YES;
    if (!isPressDetail) {
        
    
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kYearLast];
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:kMonthLast];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kTypeName];}
    isPressDetail=NO;
    
    
}
-(void)checkDetail
{
    UILabel *yearLabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kYearLabel];
    UILabel *monthLabel=(UILabel*)[self.navigationController.navigationBar viewWithTag:kMonthLabel];
    BIDBillsOfType *nect=[[BIDBillsOfType alloc] init];
    nect.typeName=_typeName.text;
    nect.month=monthLabel.text.integerValue;
    nect.year=yearLabel.text.integerValue;
    isPressDetail=YES;
    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger:nect.month] forKey:kMonthLast];
    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInteger:nect.year] forKey:kYearLast];
    [[NSUserDefaults standardUserDefaults] setValue:_typeName.text forKey:kTypeName];
   // [[NSUserDefaults standardUserDefaults] synchronize];
    [self.navigationController pushViewController:nect animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
